package com.rech.utils;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * author : haoda
 * date :  2018/12/29  18:26
 * company : 厦门翎鹿科技有限公司
 * 定时工具
 **/

public class TimerTaskUtil {
    /**
     * 定时任务
     */
    public static void timedTask(final TimerTasklenster timerTasklenster,int  time) {
        Observable.timer(time , TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        System.out.println("rxjava  d== " + disposable);
                        timerTasklenster.onSubscribe(disposable);
                    }

                    @Override
                    public void onNext(Long value) {
                        timerTasklenster.onNext();
//                        System.out.println("rxjava  value== " + value + " disposable.isDisposed() == " + timedTask.isDisposed());
                    }

                    @Override
                    public void onError(Throwable e) {
                        timerTasklenster.onError();
                        System.out.println("rxjava  Throwable== " + e.getMessage());
                        Log.d("rxjava", e.getMessage() + "");
                    }

                    @Override
                    public void onComplete() {
                        timerTasklenster.onComplete();

                        System.out.println("rxjava  onComplete 结束 ");

                    }
                });

        }
    public interface TimerTasklenster{
        public void  onNext();
        public void  onSubscribe(Disposable disposable);
        public void  onError();
        public void  onComplete();
    }
}
